package com.imooc.cloud.mall.practice.common.util;

public class MD5Utils {
    /**
     * MD5加密
     *
     * @param input 输入字符串
     * @return 加密后的字符串
     */
    public static String md5(String input) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (Exception e) {
            throw new RuntimeException("MD5加密失败", e);
        }
    }
    /**
     * MD5加密，返回小写
     *
     * @param input 输入字符串
     * @return 加密后的小写字符串
     */
    public static String md5LowerCase(String input) {
        String md5 = md5(input);
        return md5 != null ? md5.toLowerCase() : null;
    }
}
